package com.yanfan.data.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.data.domain.SnmpResponse;
import com.yanfan.data.service.ISnmpResponseService;
import com.yanfan.data.tdengine.service.TDSnmpResponseService;
import com.yanfan.iot.domain.Device;
import com.yanfan.iot.domain.Product;
import com.yanfan.iot.service.IDeviceService;
import com.yanfan.iot.service.IProductService;
import org.snmp4j.PDU;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.smi.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class SnmpJob {


    @Value("${spring.datasource.druid.tdengine-server.enabled}")
    private Boolean enabled;

    @Autowired
    private ISnmpResponseService responseService;

    @Autowired
    private TDSnmpResponseService tdSnmpResponseService;

    @Autowired
    private IProductService productService;

    @Autowired
    private SnmpServiceImpl snmpService;

    @Autowired
    private IDeviceService deviceService;



    public void snmpProductJob(Long productId){
        Product product = productService.selectProductByProductId(productId);
        //设备地址
        String ipAddress = product.getIpAddress();
        //团体名
        String community = product.getCommunity();

        String oidListStr = product.getOidListStr();
        List<JSONObject> jsonArray = JSONArray.parseArray(oidListStr, JSONObject.class);

        // 根据产品id查询设备
        List<Device> devices = deviceService.selectDeviceByProductId(productId);
        for (int i = 0; i < devices.size(); i++) {
            String serialNumber = devices.get(i).getSerialNumber();
            jsonArray.stream().forEach(item ->{
                ResponseEvent res = snmpService.getMessageByIpAndOid(ipAddress, item.get("oid").toString(), community);
                if (null != res && null != res.getResponse()) {
                    PDU response = res.getResponse();
                    Address peerAddress = res.getPeerAddress();

                    SnmpResponse snmpResponse = new SnmpResponse();
                    snmpResponse.setPeerAddress(peerAddress.toString());
                    snmpResponse.setOidName(item.get("oidName").toString());
                    snmpResponse.setSerialNumber(serialNumber);

                    for (int j = 0; j < response.size(); j++) {
                        // 获取响应结果
                        VariableBinding vb = response.get(j);
                        snmpResponse.setOid(vb.getOid().toString());
                        snmpResponse.setVariable(vb.getVariable().toString());
                        if (enabled) {
                            // 生成19位的唯一id
                            Snowflake snowflake = IdUtil.getSnowflake(6, 2);
                            Long id = snowflake.nextId();
                            snmpResponse.setId(id.toString());

                            // 使用时序数据库保存数据
                            tdSnmpResponseService.insertSnmpResponse(snmpResponse);

                        } else {
                            responseService.insertSnmpResponse(snmpResponse);
                        }
                    }
                }
            });
        }

    }
}
